//
//  HomeViewController.m
//  MtimeMovie
//
//  Created by lllyyy on 15/7/17.
//  Copyright (c) 2015年 lllyyy. All rights reserved.
//

#import "HomeViewController.h"
#import "MovieModel.h"
#import "MovieCell.h"
@interface HomeViewController ()
{
    BOOL isLeft;
}

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [self _setNavigation];
    [self _createMovieTableView];
    [self _createPosterView];
    [self _loadData];
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    // Do any additional setup after loading the view.
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if(self){
        self.title = @"首页";
    }
    return self;
}


- (void)_loadData{
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"us_box.json" ofType:nil];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    _movieModalArray = [[NSMutableArray alloc]init];
    
    
    NSArray *subjects = [dataDic objectForKey:@"subjects"];
    for (NSDictionary *dic in subjects) {
        NSDictionary *subject = [dic objectForKey:@"subject"];
        MovieModel *modal = [[MovieModel alloc]init];
        modal.title = [subject objectForKey:@"title"];
        modal.year = [subject objectForKey:@"year"];
        modal.images = [subject objectForKey:@"images"];
        NSNumber *number = [[subject objectForKey:@"rating"]objectForKey:@"average"];
        modal.average = [number floatValue];
        [_movieModalArray addObject:modal];
       // NSLog(@"%@",_movieModalArray);
    }
}

- (void)_setNavigation{
    
    UIView *flipView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithCustomView:flipView];
    
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setImage:[UIImage imageNamed:@"list_home"] forState:UIControlStateNormal];
    [btn1 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    btn1.tag = 1;
    
    [btn1 sizeToFit];
    [btn1 addTarget:self action:@selector(filpAction:) forControlEvents:UIControlEventTouchUpInside];
    [flipView addSubview:btn1];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btn2 setImage:[UIImage imageNamed:@"poster_home"] forState:UIControlStateNormal];
    [btn2 setBackgroundImage:[UIImage imageNamed:@"exchange_bg_home"] forState:UIControlStateNormal];
    btn2.tag = 2;
    [btn2 sizeToFit];
    [btn2 addTarget:self action:@selector(filpAction:) forControlEvents:UIControlEventTouchUpInside];
    [flipView addSubview:btn2];
    btn2.hidden = YES;

    self.navigationItem.rightBarButtonItem = item1;
    
//    self.title = @"首页";
//    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(editAction)];
//    item.image = [UIImage imageNamed:@"list_home"];
//    item.tintColor = [UIColor whiteColor];
//    [item setBackgroundImage:[UIImage imageNamed:@"selectTabBar_bg_all1"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
//    self.navigationItem.rightBarButtonItem = item;
    
}

- (void)_createPosterView{
    _posterView = [[UIView alloc]initWithFrame:self.view.bounds];
    _posterView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_posterView];
}
- (void)_createMovieTableView{
    _movieTableView = [[UITableView alloc]initWithFrame:self.view.bounds];
    _movieTableView.dataSource = self;
    _movieTableView.delegate = self;
    _movieTableView.hidden = YES;
    [self.view addSubview:_movieTableView];
    
    UINib *nib = [UINib nibWithNibName:@"MovieCell" bundle:[NSBundle mainBundle]];
    [_movieTableView registerNib:nib forCellReuseIdentifier:@"cell"];
}

- (void)filpAction:(UIButton *)button{
    
    UIView *filpView = self.navigationItem.rightBarButtonItem.customView;
    
    UIButton *btn1 = (UIButton *)[filpView viewWithTag:1];
    UIButton *btn2 = (UIButton *)[filpView viewWithTag:2];

    isLeft = !isLeft;
    
    [self _flipView:filpView ];
    [self _flipView:self.view ];
    
    btn1.hidden = !btn1.hidden;
    btn2.hidden = !btn2.hidden;
    
    _posterView.hidden = !_posterView.hidden;
    _movieTableView.hidden = !_movieTableView.hidden;
}

- (void)_flipView:(UIView *)view{
    
    UIViewAnimationOptions option = isLeft?UIViewAnimationOptionTransitionFlipFromLeft:UIViewAnimationOptionTransitionFlipFromRight;


    [UIView transitionWithView:view duration:.5 options:option animations:nil completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _movieModalArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    MovieModel *modal = _movieModalArray[indexPath.row];
    
    cell.modal = modal;
    
    return cell;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
